#-*- coding:gbk -*-
import os
import os.path
import MySQLdb
import datetime
import gc
from multiprocessing import Pool

def long_time_task(sql,fname):

    conn= MySQLdb.connect(
            host='localhost',
            port = 3306,
            user='root',
            passwd='111qqq!',
            db ='test',
            charset='gbk'
            )
    cur = conn.cursor()                                  
    sqlsu = "UPDATE test set success = 1 where id=(%s)"
    begin = datetime.datetime.now()
    path = cur.execute(sql)
    f = open(fname, "w")
    print "watch message.log ..."
    for mkdir in cur.fetchmany(path):
        id = mkdir[0]
        olddir = mkdir[1]
        newdir ="d:\\test\\"+olddir[3:]
        print >> f, "%s %d" % ("recentId:",id)
        if not os.path.exists(newdir):
            #print >> f, "%s %d" % ("lostId:",id)
            try:
                cur.execute(sqlsu,(id))
            except Exception,e:
                print >> f, "%s" % (e)    
    f.close()
    end = datetime.datetime.now()
    
    print end-begin
    cur.close()
    conn.commit()
    conn.close()
    
if __name__=='__main__':
    sql1 = "SELECT id,path FROM test order by id asc limit 19820"
    sql2 = "SELECT id,path FROM test order by id desc limit 19821"
    fname1 = "cm1.log"
    fname2 = "cm2.log"
    p = Pool()
    p.apply_async(long_time_task, args=(sql1,fname1))
    p.apply_async(long_time_task, args=(sql2,fname2))
    p.close()
    p.join()
    print 'All subprocesses done.'

